package com.example.financialsystem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.commons.lang3.StringUtils;

import models.User;
import models.UserVO;
import okhttp3.Request;
import okhttp3.RequestBody;
import utils.GsonUtil;
import others.ServerResponse;
import utils.OkHttpCallback;
import utils.OkhttpUtil;
import utils.SharedPreferencesUtil;

import static others.Const.CURRENT_USER;
import static others.Const.IP;
import static others.Const.JSON;
import static others.Const.PORT;
import static others.Const.currentUserVo;
import static others.Tools.ToastShow;
import static utils.GsonUtil.String2UserVoServerResponse;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView buttonRegister;
    private Button buttonLogin;
    private EditText editUsername;
    private EditText editPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        buttonRegister = findViewById(R.id.login_register);
        buttonRegister.setOnClickListener(this);
        buttonLogin = findViewById(R.id.login);
        buttonLogin.setOnClickListener(this);
        editUsername = findViewById(R.id.login_username);
        editPassword = findViewById(R.id.login_password);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                String username = editUsername.getText().toString();
                String password = editPassword.getText().toString();
                if (!StringUtils.isBlank(username) && !StringUtils.isBlank(password)) {
                    User loginUser = new User(username,password);
                    String jsonData = GsonUtil.class2Json(loginUser);
                    String url = "http://" + IP + ":" + PORT + "/portal/user/login.do";
                    sendRequest(url, jsonData);
                }else {
                    ToastShow("账号或密码不能为空");
                }
                break;
            case R.id.login_register:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                finish();
                break;
            default:
                break;
        }
    }

    public void sendRequest(final String url, final String data){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    RequestBody requestBody = RequestBody.create(JSON,data);
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody)
                            .build();
                    OkhttpUtil.SendRequestWithPost(request,
                            new OkHttpCallback(){
                                @Override
                                public void onFinish(String status, String responseData) {
                                    if (status.equals("Success")){
                                        ServerResponse<UserVO> serverResponse = String2UserVoServerResponse(responseData);
                                        if (serverResponse.getStatus() ==  0){
                                            Intent intent = new Intent(LoginActivity.this,HomeScreenActivity.class);
                                            intent.putExtra(CURRENT_USER,serverResponse.getData());
                                            currentUserVo = serverResponse.getData();
                                            startActivity(intent);
                                            finish();
                                        }else {
                                            Looper.prepare();
                                            ToastShow(serverResponse.getStatus()+" "+serverResponse.getMsg());
                                            Looper.loop();
                                        }
                                    }else {
                                        Looper.prepare();
                                        ToastShow("连接服务器失败,请稍后再试");
                                        Looper.loop();
                                    }
                                }
                            });
                }catch (Exception e){
                    ToastShow("抱歉了亲,我们可能遇到了些麻烦");
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        }
    };


}
